package goblin.game;

import engine.game.Game;
import engine.game.sfx.LoopSound;
import engine.resource.ResourceInputFile;
import java.nio.file.Path;
import java.nio.file.Paths;
import util.Logger;

/**
 * The main class for the Goblin Menace game
 *
 * @author Nathan T
 *
 */
public class GoblinMenace implements Runnable {

    public static Logger logger = Logger.getDefaultLogger(); // A logger that will record what happens in the game

    /**
     * The main method of the Goblin Menace game. It creates a new Goblin Menace
     * game and runs it.
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        GoblinMenace menace = new GoblinMenace();
        new Thread(menace).start();
    }

    /**
     * The default constructor.
     */
    public GoblinMenace() {
        buildResources();
    }

    /**
     * Runs the Goblin Menace game.
     */
    @Override
    public void run() {
        try {
            Game game = new Game();
            game.start();

            while (game.isRunning()) {
                Thread.yield();
                Thread.sleep(10);
            }
        }
        catch (Exception e) {
            logger.log(e);
        }
        finally {
            logger.close();
            
            // This is kind of hacky - need to change it to proper thread handling
            System.exit(0);
        }
    }

    /**
     * Compiles the resources necessary to run the game and loads them to the
     * Resource class.
     */
    private void buildResources() {
        // Calculate the location of the .jar file this is running from, to use for the construction of resource loading paths
        Path currentRelativePath = Paths.get("");
        String path = currentRelativePath.toAbsolutePath().toString();

        // Read in the data from the resource input file
        ResourceInputFile res = new ResourceInputFile(path + "/data/resources.ini");
        res.readData();

        // Start looping the primary soundtrack
        LoopSound.main(new String[1]);
    }

}
